package Servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import DAO.NguoiDungDAO;
import POJO.NguoiDung;

/**
 * Servlet implementation class MyServlet
 */
// @WebServlet("/MyServlet")
public class DangNhapServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DangNhapServlet()
    {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        if((String) request.getParameter("out") != null)
        {
            HttpSession session = request.getSession();
            session.invalidate();
            
            RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
            rd.forward(request, response);
        }
        else
        {
            RequestDispatcher rd = request.getRequestDispatcher("/view/login.jsp");
            rd.forward(request, response);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        String username = null;
        String pass = null;

        // Lấy username và password
        username = request.getParameter("txt_user");
        pass = request.getParameter("txt_password");
        
        //Kiểm tra đăng nhập
        if (ktDangNhap(username, pass))
        {
          //Tạo Session
            HttpSession session = request.getSession();
            session.setAttribute("nguoiDung", nd);
            session.setAttribute("id", "1");
            
         // Thực hiện chuyển lại trang chủ            
            response.sendRedirect("/OnlineShop/index.jsp");
        }
        else
        {
            response.sendRedirect("login?err=0");
            // thực hiện chuyện lại trang đăng nhập với biến err có kiểu là int
        }
    }
    
    private static NguoiDung nd = new NguoiDung();
    
    private static boolean ktDangNhap(String username, String pass) throws UnsupportedEncodingException
    {
      //chuyển sang dạng có dấu
        username = new String(username.getBytes("ISO-8859-1"),"UTF-8");
        
        nd = NguoiDungDAO.SelectNguoiDungByNameAndPassword(username, pass);
        
        if (!(nd.getTenNguoiDung()!= null && nd.getTenNguoiDung().equals(username) && nd.getMatKhau().equals(pass)))
            return false;
        return true;
    }

}
